import java.io.File;
import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2023-10-28
 * Time: 16:08
 */
public class practice1 {
    public static Scanner scanner = new Scanner(System.in);
    public static void main(String[] args) {
        //1.先输入要查找的目录
        System.out.println("请输入要搜索的目录：");
        File rootPath  = new File(scanner.next());
        //2.输入要删除的关键字
        System.out.println("请输入要删除的关键字：");
        String word = scanner.next();
        //3.判断该目录是否合法
        if (!rootPath .isDirectory()) {
            System.out.println("您输入的路径不是合法目录！！");
            return;
        }
        //4.开始搜索该目录
        System.out.println("开始遍历搜索该目录");
        scanDir(rootPath, word);
    }

    public static void scanDir (File currentDir, String word) {
        File[] files = currentDir.listFiles();
        if(files == null || files.length ==0 ) {
            //空的目录或者非法的目录
            return;
        }
        for (File f : files) {
            System.out.println(f.getAbsolutePath());
            if(f.isFile()) {
                dealFile(f, word);
            } else {
                scanDir(f,word);
            }
        }
    }

    public static void dealFile (File currentFile, String word) {
        if(!currentFile.getName().contains(word)) {
            return;
        }
        System.out.println("该文件是：" + currentFile.getAbsolutePath() + ", 是否要确认删除？（Y/N）");
        String choice = scanner.next();
        if(choice.equals("Y") || choice.equals("y")) {
            currentFile.delete();
            System.out.println("删除成功！！");
        }

        //如果是其他值都忽略
    }
}
